# 项目文件夹和目录变量
PROJECT_PATH = /projects/volcano-domain-site-workbox
STATIC_DIR = $(PROJECT_PATH)/static
UPLOADS_DIR = $(STATIC_DIR)/uploads
MODELS_DIR = $(PROJECT_PATH)/custom_models
OUTPUT_DIR = $(PROJECT_PATH)/output_files
LOGS_DIR = $(PROJECT_PATH)/logs
GITEE_REPO = https://gitee.com/aylerh/volcano-domain-site-workbox

# 默认目标
.PHONY: all
all: create_dirs

# 首次克隆项目并设置
.PHONY: first_clone
first_clone:
	@echo "开始首次克隆和设置项目..."
	@if [ ! -d "/projects" ]; then \
		mkdir -p /projects; \
		echo "创建/projects目录"; \
	fi
	@if [ -d "$(PROJECT_PATH)" ]; then \
		echo "警告: 项目目录已存在: $(PROJECT_PATH)"; \
		echo "如需重新克隆，请先手动删除该目录"; \
	else \
		echo "克隆项目仓库..."; \
		git clone $(GITEE_REPO) $(PROJECT_PATH); \
		echo "项目克隆完成"; \
		echo "赋予deploy.sh执行权限..."; \
		chmod +x $(PROJECT_PATH)/deploy.sh; \
		echo "首次克隆设置完成，您现在可以:"; \
		echo "cd $(PROJECT_PATH)"; \
		echo "sudo ./deploy.sh"; \
	fi

# 拉取最新代码
.PHONY: pull-code
pull-code:
	@echo "开始拉取最新代码..."
	@if [ ! -d "$(PROJECT_PATH)" ]; then \
		echo "错误: 项目目录不存在: $(PROJECT_PATH)"; \
		echo "请先使用 'make first_clone' 克隆项目"; \
		exit 1; \
	fi
	@echo "进入项目目录..."
	@cd $(PROJECT_PATH) && \
	echo "重置所有本地修改..." && \
	git reset --hard && \
	echo "拉取最新代码..." && \
	git pull && \
	echo "赋予deploy.sh执行权限..." && \
	chmod +x $(PROJECT_PATH)/deploy.sh && \
	echo "代码更新完成"

# 重启服务
.PHONY: restart
restart:
	@echo "重启服务..."
	@if ! systemctl is-active --quiet work8; then \
		echo "服务未运行，正在启动..."; \
		sudo systemctl start work8; \
	else \
		echo "重启work8服务..."; \
		sudo systemctl restart work8; \
	fi
	@echo "重启Nginx..."
	@sudo systemctl restart nginx
	@echo "检查服务状态..."
	@sudo systemctl status work8 --no-pager
	@echo "服务重启完成"

# 运行部署脚本（跳过克隆步骤）
.PHONY: deploy-no-clone
deploy-no-clone:
	@echo "开始执行部署脚本（跳过克隆步骤）..."
	@if [ ! -d "$(PROJECT_PATH)" ]; then \
		echo "错误: 项目目录不存在: $(PROJECT_PATH)"; \
		echo "请先使用 'make first_clone' 克隆项目"; \
		exit 1; \
	fi
	@if [ ! -x "$(PROJECT_PATH)/deploy.sh" ]; then \
		echo "错误: deploy.sh 不存在或没有执行权限"; \
		echo "请检查文件是否存在并设置权限"; \
		exit 1; \
	fi
	@cd $(PROJECT_PATH) && sudo SKIP_CLONE=true ./deploy.sh

# 运行部署脚本（包含克隆步骤）
.PHONY: deploy
deploy:
	@echo "开始执行部署脚本..."
	@if [ ! -d "$(PROJECT_PATH)" ]; then \
		echo "错误: 项目目录不存在: $(PROJECT_PATH)"; \
		echo "请先使用 'make first_clone' 克隆项目"; \
		exit 1; \
	fi
	@if [ ! -x "$(PROJECT_PATH)/deploy.sh" ]; then \
		echo "错误: deploy.sh 不存在或没有执行权限"; \
		echo "请检查文件是否存在并设置权限"; \
		exit 1; \
	fi
	@cd $(PROJECT_PATH) && sudo ./deploy.sh

# 更新现有项目并部署（跳过克隆步骤）
.PHONY: update
update: pull-code deploy-no-clone restart

# 创建所需的目录结构
.PHONY: create_dirs
create_dirs:
	@echo "检查并创建必要的目录结构..."
	@if [ ! -d "$(PROJECT_PATH)" ]; then \
		mkdir -p $(PROJECT_PATH); \
		echo "创建项目目录: $(PROJECT_PATH)"; \
	fi
	@if [ ! -d "$(STATIC_DIR)" ]; then \
		mkdir -p $(STATIC_DIR); \
		echo "创建静态文件目录: $(STATIC_DIR)"; \
	fi
	@if [ ! -d "$(UPLOADS_DIR)" ]; then \
		mkdir -p $(UPLOADS_DIR); \
		echo "创建上传目录: $(UPLOADS_DIR)"; \
		mkdir -p $(UPLOADS_DIR)/$(shell date +"%Y/%m"); \
		echo "创建当月上传子目录: $(UPLOADS_DIR)/$(shell date +"%Y/%m")"; \
	fi
	@if [ ! -d "$(MODELS_DIR)" ]; then \
		mkdir -p $(MODELS_DIR)/det $(MODELS_DIR)/rec $(MODELS_DIR)/cls; \
		echo "创建模型目录: $(MODELS_DIR)"; \
	fi
	@if [ ! -d "$(OUTPUT_DIR)" ]; then \
		mkdir -p $(OUTPUT_DIR)/temp; \
		echo "创建输出文件目录: $(OUTPUT_DIR)"; \
	fi
	@if [ ! -d "$(LOGS_DIR)" ]; then \
		mkdir -p $(LOGS_DIR); \
		echo "创建日志目录: $(LOGS_DIR)"; \
	fi
	@echo "目录结构检查完成"

# 清理临时文件
.PHONY: clean
clean:
	@echo "清理临时文件..."
	@rm -rf $(OUTPUT_DIR)/temp/*
	@if [ -d "/dev/shm/pdf_temp" ]; then \
		rm -rf /dev/shm/pdf_temp/*; \
		echo "清理/dev/shm/pdf_temp临时文件"; \
	fi
	@echo "临时文件已清理"

# 添加swap空间
.PHONY: add-swap
add-swap:
	@echo "检查是否已存在swap..."
	@if grep -q "/swapfile" /proc/swaps; then \
		echo "Swap已存在，当前swap状态:"; \
		free -h | grep -i swap; \
	else \
		echo "创建2GB swap文件..."; \
		sudo fallocate -l 2G /swapfile || sudo dd if=/dev/zero of=/swapfile bs=1M count=2048; \
		sudo chmod 600 /swapfile; \
		sudo mkswap /swapfile; \
		sudo swapon /swapfile; \
		echo "添加swap到/etc/fstab使其永久生效..."; \
		if ! grep -q "/swapfile" /etc/fstab; then \
			echo "/swapfile swap swap defaults 0 0" | sudo tee -a /etc/fstab; \
		fi; \
		echo "Swap创建完成，当前状态:"; \
		free -h | grep -i swap; \
	fi

# 显示帮助信息
.PHONY: help
help:
	@echo "可用命令:"
	@echo "  make              - 检查并创建必要的目录结构"
	@echo "  make first_clone  - 首次克隆项目并进行初始设置"
	@echo "  make pull-code    - 仅拉取最新代码"
	@echo "  make deploy       - 运行部署脚本（包含克隆步骤）"
	@echo "  make deploy-no-clone - 运行部署脚本（跳过克隆步骤）"
	@echo "  make update       - 拉取最新代码并运行部署脚本（跳过克隆步骤）"
	@echo "  make restart      - 重启服务（不重新部署）"
	@echo "  make clean        - 清理临时文件"
	@echo "  make add-swap     - 添加swap空间（2GB）以提高低内存环境性能"
	@echo "  make help         - 显示此帮助信息"